package com.woniuxy.riskmanage.service.impl;

import com.woniuxy.commons.entity.RuleLog;
import com.woniuxy.commons.utils.GetIp;
import com.woniuxy.riskmanage.mapper.RuleLogMapper;
import com.woniuxy.riskmanage.service.RuleLogService;
import org.aspectj.lang.JoinPoint;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author author
 * @create 2021-11-13 14:54
 */
@Service
public class RuleLogServiceImpl implements RuleLogService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private RuleLogMapper ruleLogMapper;


    @Override
    public void addRuleLog(JoinPoint joinPoint, String methodName, String module, String description) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = GetIp.getIpAddress(request);
        String username = (String) redisTemplate.opsForValue().get("username");
        RuleLog ruleLog = new RuleLog();
        ruleLog.setUsername(username);
        ruleLog.setOperationtime(new Date());
        ruleLog.setContent(description);
        ruleLog.setModule(module);
        ruleLog.setHostip(ip);
        ruleLogMapper.addRuleLog(ruleLog);
    }

    @Override
    public List<RuleLog> getAllRuleLog(Integer pageIndex, Integer pageSize) {
        List<RuleLog> allRuleLog = ruleLogMapper.getAllRuleLog(pageIndex, pageSize);
        return allRuleLog;
    }

    @Override
    public void delRuleLog(int logid) {
        ruleLogMapper.delRuleLog(logid);
    }

    @Override
    public int selectRuleLogCount() {
        return ruleLogMapper.selectRuleLogCount();
    }
}
